package com.aop.SpElAop.DistributedLock.RedisLock.LockType;

/**
 * @Package: com.aop.SpElAop.Lock
 * @ClassName: WriteLock
 * @Author: h2cloud
 * @CreateTime: 2024/7/9 10:08 下午
 * @Description:
 */
import com.aop.SpElAop.DistributedLock.RedisLock.support.LockInfo;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;

@Slf4j
public class WriteLock extends AbstractLock {
	public WriteLock(RedissonClient redissonClient, LockInfo lockInfo) {
		super(redissonClient, lockInfo);
	}

	@Override
	protected RLock getRedissonLock() {
		if (getLockInfo().isMultiLock()) {
			RLock[] rLocks = new RLock[getLockInfo().getMultiLockNames().size()];
			for (int i = 0; i < getLockInfo().getMultiLockNames().size(); i++) {
				rLocks[i] = getRedissonClient().getReadWriteLock(getLockInfo().getMultiLockNames().get(i)).writeLock();
			}
			return getRedissonClient().getMultiLock(rLocks);
		} else {
			return getRedissonClient().getReadWriteLock(getLockInfo().getName()).writeLock();
		}
	}
}

